//
//  main.m
//  Demo0017
//
//  Created by zTercel on 13-1-16.
//  Copyright (c) 2013年 zTercel. All rights reserved.
//

#import <Foundation/Foundation.h>



//Creating a Dictionary
//------------------------------------
//+ dictionary
//+ dictionaryWithDictionary:
//+ dictionaryWithObject:forKey:
//+ dictionaryWithObjects:forKeys:count:
//+ dictionaryWithObjectsAndKeys:

//Initializing an NSDictionary Instance
//------------------------------------
//– initWithDictionary:
//– initWithObjects:forKeys:count:
//– initWithObjectsAndKeys:

void print(NSDictionary* dictionary)
{
    NSMutableString* prompt     = [NSMutableString stringWithFormat:@"\nobject count in dirctionary: %ld\n", [dictionary count]];
    [prompt appendFormat:@"%-20s %-20s\n", "Key", "value"];
    [prompt appendFormat:@"--------------------------------------\n"];
    for (NSString* key in dictionary)
    {
        [prompt appendFormat:@"%-20s %-20s\n", [key UTF8String], [[dictionary objectForKey:key] UTF8String]];
    }
    NSLog(@"%@", prompt);
    
    return;
}

void createDictionary()
{
    {
        NSDictionary* dictionary    = [NSDictionary dictionary];
        print(dictionary);
    }
    
    {
        NSDictionary* dictionary    = [NSDictionary dictionaryWithObject:@"132-1248-2384" forKey:@"zTercel"];
        print(dictionary);
        NSDictionary* dictionary1   = [NSDictionary dictionaryWithDictionary:dictionary];
        print(dictionary1);
    }
    
    {
        NSDictionary* dictionary    = [NSDictionary dictionaryWithObjectsAndKeys:@"134-2323-4421", @"yTercel", @"154-2343-5683", @"malili", @"189-2484-9573", @"lilei", nil];
        print(dictionary);
    }
    
    {
        NSString* arrayKey[2]     = {@"malili", @"yangmei"};
        NSString* arrayValue[2]   = {@"189-2321-4563", @"187-3482-8526"};
        
        NSDictionary* dictionary    = [NSDictionary dictionaryWithObjects:(id*)arrayValue forKeys:(id*)arrayKey count: sizeof(arrayKey)/sizeof(*arrayKey)];
        print(dictionary);
    }
    return;
}

//Accessing Keys and Values
//------------------------------------
//– objectForKey:
//– valueForKey:
void accessDictionary()
{
    NSDictionary* dictionary    = [NSDictionary dictionaryWithObjectsAndKeys:@"134-2323-4421", @"yTercel", @"154-2343-5683", @"malili", @"189-2484-9573", @"lilei", nil];
    print(dictionary);
    
    NSLog(@"%@ value: %@", @"lilei", [dictionary valueForKey:@"lilei"]);
    NSLog(@"%@ value: %@", @"lilei", [dictionary objectForKey:@"lilei"]);

    return;
}

//Comparing Dictionaries
//------------------------------------
void sortDictionary()
{
    NSDictionary* dictionary    = [NSDictionary dictionaryWithObjectsAndKeys:@"135-2323-4421", @"yTercel", @"154-2343-5683", @"malili", @"189-2484-9573", @"lilei", @"132-3422-3847", @"hanlei", nil];
    print(dictionary);
    
    NSArray *arraySort = [dictionary keysSortedByValueUsingComparator: ^(id obj1, id obj2) {
        if ([obj1 compare: obj2] == NSOrderedDescending)
            return (NSComparisonResult)NSOrderedDescending;
        if ([obj1 compare: obj2] == NSOrderedAscending)
            return (NSComparisonResult)NSOrderedAscending;
        return (NSComparisonResult)NSOrderedSame;
    }];
    
    for (NSString* key in arraySort) {
        NSLog(@"key = %@  value: %@", key, [dictionary objectForKey:key]);
    }
    
}








//– isEqualToDictionary:


//Enumerating Dictionaries
//------------------------------------
//– keyEnumerator
//– objectEnumerator
//– enumerateKeysAndObjectsUsingBlock:
//– enumerateKeysAndObjectsWithOptions:usingBlock:

//Sorting Dictionaries
//------------------------------------
//– keysSortedByValueUsingSelector:
//– keysSortedByValueUsingComparator:
//– keysSortedByValueWithOptions:usingComparator:

//Filtering Dictionaries
//------------------------------------
//– keysOfEntriesPassingTest:
//– keysOfEntriesWithOptions:passingTest:



int main(int argc, const char * argv[])
{
    @autoreleasepool {
        createDictionary();
        accessDictionary();
        sortDictionary();
    }
    return 0;
}

